package vip.wangwenhao.util;

import org.apache.commons.collections.CollectionUtils;
import org.codehaus.plexus.util.StringUtils;
import vip.wangwenhao.mojo.GeneralMojo;
import vip.wangwenhao.vo.ColumnInfo;
import vip.wangwenhao.vo.IndexInfo;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wwh
 * @date 2019年12月04日 16:26
 */

public class GenMapperUtil {

    public static List<String> tablePrefixes;
    public static List<String> columnPrefixes;
    public static String mapperPackage;
    public static String beanPackage;
    public static String author;

    public static void init(GeneralMojo generalMojo) {
        tablePrefixes = generalMojo.getTablePrefixes();
        columnPrefixes = generalMojo.getColumnPrefixes();
        mapperPackage = generalMojo.getMapperPackage();
        beanPackage = generalMojo.getBeanPackage();
        String author = generalMojo.getAuthor();
        if (StringUtils.isEmpty(author)) {
            author = "";
        }
        GenMapperUtil.author = author;
    }

    public static void createMapper(String tableName) {

        String beanName = StringUtil.initCap(tableName, true, tablePrefixes);
        String paramName = StringUtil.initCap(tableName, false, tablePrefixes);
        String part = "";
        StringBuffer paramList = new StringBuffer();
        List<ColumnInfo> tablePrimary = DatabaseUtil.getTablePrimary(tableName);
        if (CollectionUtils.isNotEmpty(tablePrimary)) {
            for (ColumnInfo columnInfo : tablePrimary) {
                paramList.append(columnInfo.getColumnType().getJavaType() + " " + StringUtil.initCap(columnInfo.getColumnName(), false, columnPrefixes) + ",");
            }
            paramList.deleteCharAt(paramList.length() - 1);

        }
        if (StringUtils.isNotBlank(paramList.toString())) {
            part =
                            "    int delete(" + paramList.toString() + ");\n" +
                            "\n" +
                            "    " + beanName + " selectByPrimaryKey(" + paramList.toString() + ");\n";
        }
        StringBuffer indexPart = new StringBuffer();
        List<IndexInfo> indexInfos = DatabaseUtil.getIndex(tableName);
        if (CollectionUtils.isNotEmpty(indexInfos)){
            indexInfos.forEach(indexInfo->{

                String paramListName = String.join("And", indexInfo.getColumnNames().stream().map( columnName-> StringUtil.initCap(columnName,true,columnPrefixes)).collect(Collectors.toList()));


                if (!"PRIMARY".equals(indexInfo.getIndexName())){
                    List<ColumnInfo> v = indexInfo.getColumnInfos();
                    StringBuffer indexParamList = new StringBuffer();
                    if (CollectionUtils.isNotEmpty(v)) {
                        for (ColumnInfo columnInfo : v) {
                            indexParamList.append(columnInfo.getColumnType().getJavaType() + " " + StringUtil.initCap(columnInfo.getColumnName(), false, columnPrefixes) + ",");
                        }
                        indexParamList.deleteCharAt(indexParamList.length() - 1);

                    }
                    if (StringUtils.isNotBlank(indexParamList.toString())) {
                        if (indexInfo.getUnique()){
                            indexPart .append(
                                            "    " + beanName + " selectBy"+ paramListName +"(" + indexParamList.toString() + ");\n\n");
                        }else {
                            indexPart .append(
                                            "    List<" + beanName + "> selectBy"+ paramListName +"(" + indexParamList.toString() + ");\n\n");
                        }
                    }
                }
            });
        }
        String content =
                "package " + mapperPackage + ";\n" +
                        "import " + beanPackage + "." + beanName + ";\n" +
                        "import java.util.List;\n" +
                        "\n" +
                        "/**\n" +
                        "*  \n" +
                        "*\n" +
                        "* @author " + author + "\n" +
                        "* \n" +
                        "*/\n" +
                        "    \n" +
                        "public interface " + beanName + "DAO {\n" +
                        "    \n" +
                        "    int insert(" + beanName + " " + paramName + ");\n" +
                        "\n" +
                        "    int update(" + beanName + " " + paramName + ");\n" +
                        "\n" +
                        "    List<" + beanName + "> query(" + beanName + " " + paramName + ");" +
                        "\n" +
                        "\n" +
                        part +
                        "\n" +
                        indexPart.toString() +
                        "}";

        FileUtil.write(content, "java" + File.separator + mapperPackage.replace(".", File.separator), beanName + "DAO.java");
    }


}